# @Time:24-12-16
# @Author: niqiang
# @Function:
from PyQt5.QtWidgets import QApplication, QStyleOptionViewItem, QWidget, QLabel, QComboBox
from PyQt5.QtWidgets import QFileDialog, QListView, QTreeView, QItemDelegate, QPushButton
from PyQt5.QtWidgets import QLineEdit, QSplitter, QToolButton
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtCore import Qt, QSize
import sys
import signal

# 定义全局常量
FONT_SIZE = 20
ICON_SIZE = QSize(80, 80)
LABEL_HEIGHT = 80
TOOLBUTTON_WIDTH = 60
TOOLBUTTON_HEIGHT = 80

class CustomFileDialog(QFileDialog):
    """
    自定义文件对话框类，继承自QFileDialog。
    该类用于调整对话框中各种UI组件的样式和属性，以满足特定的UI需求。
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setOption(QFileDialog.DontUseNativeDialog, True)  # 使用非本地对话框以自定义样式
        self.list_all_children()
           
        # 调整所有控件的高度和字体大小
        self.adjust_list_view()
        self.adjust_tree_view()
        self.adjust_labels()
        self.adjust_edits()
        self.adjust_comboboxes()
        self.adjust_pushbuttons()
        self.adjust_toolbuttons()
        self.adjust_splitter()

    """
    列出所有子组件的类型和对象名称。
    用于调试和确认所有子组件的属性。
    """
    def list_all_children(self):
        children = self.findChildren(QWidget)
        for child in children:
            print(f"Type: {type(child).__name__}, Object Name: {child.objectName()}")

    def adjust_list_view(self):
        listviews = self.findChildren(QListView)
        for listview in listviews:
            listview.setItemDelegate(CustomItemDelegate(self))
            font = QFont()
            font.setPointSize(FONT_SIZE)
            listview.setFont(font)
            listview.setIconSize(ICON_SIZE)

    def adjust_tree_view(self):
        treeviews = self.findChildren(QTreeView)
        for treeview in treeviews:
            treeview.setItemDelegate(CustomItemDelegate(self))
            font = QFont()
            font.setPointSize(FONT_SIZE)
            treeview.setFont(font)
            treeview.setIconSize(ICON_SIZE)

    def adjust_labels(self):
        labels = self.findChildren(QLabel)
        for label in labels:
            label.hide()

    def adjust_edits(self):
        line_edits = self.findChildren(QLineEdit)
        for line_edit in line_edits:
            font = QFont()
            font.setPointSize(FONT_SIZE)
            line_edit.setFont(font)
            line_edit.setFixedHeight(LABEL_HEIGHT)
            line_edit.setReadOnly(True)

    def adjust_comboboxes(self):
        font = QFont()
        font.setPointSize(FONT_SIZE)
        comboboxes = self.findChildren(QComboBox)
        for combobox in comboboxes:
            combobox.setFont(font)
            combobox.setFixedHeight(LABEL_HEIGHT)
    
    def adjust_pushbuttons(self):
        font = QFont()
        font.setPointSize(FONT_SIZE)
        buttons = self.findChildren(QPushButton)
        for button in buttons:
            button.setFont(font)
            button.setFixedHeight(LABEL_HEIGHT)
            icon = button.icon()
            if not icon.isNull():
                button.setIcon(QIcon())

    def adjust_toolbuttons(self):
        font = QFont()
        font.setPointSize(FONT_SIZE)
        toolbuttons = self.findChildren(QToolButton)
        for toolbutton in toolbuttons:
            if toolbutton.objectName() == "newFolderButton":
                toolbutton.hide()
                continue
            toolbutton.setFont(font)
            toolbutton.setFixedWidth(TOOLBUTTON_WIDTH)
            toolbutton.setFixedHeight(TOOLBUTTON_HEIGHT)
            icon = toolbutton.icon()
            if not icon.isNull():
                available_sizes = icon.availableSizes()
                if QSize(64, 64) in available_sizes:
                    pixmap = icon.pixmap(QSize(64, 64))
                else:
                    pixmap = icon.pixmap(available_sizes[0]).scaled(QSize(64, 64), Qt.KeepAspectRatio, Qt.SmoothTransformation)
                toolbutton.setIcon(QIcon(pixmap))

        """
        调整最左侧目录树的大小，使其宽度增加180像素。
        """
    def adjust_splitter(self):
        splitters = self.findChildren(QSplitter)
        for splitter in splitters:
            current_sizes = splitter.sizes()
            if current_sizes:
                new_size = current_sizes[0] + 180
                new_sizes = [new_size] + current_sizes[1:]
                splitter.setSizes(new_sizes)

class CustomItemDelegate(QItemDelegate):
    def sizeHint(self, option: QStyleOptionViewItem, index) -> QSize:
        size = super().sizeHint(option, index)
        size.setHeight(LABEL_HEIGHT)
        return size

if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    app = QApplication(sys.argv)
    dialog = CustomFileDialog()
    dialog.setGeometry(0,0,0,0)
    dialog.setNameFilter("H264 Files (*.264, *.mp4)")
    dialog.show()
    print(dialog.selectedFiles())
    sys.exit(app.exec_())